<?php
include_once '../config/koneksi.php';

class Users{
var $nip;
var $nama;
var $pass;
var $bagian;
var $jabatan;
var $admin;
protected $connect;

        function __construct(){
            $this->connect = new Koneksi();
            $this->connect->connect();

        }
    
        //fungsi untuk mendapatkan user dari database
        //digunakan untuk login dan menciptakan sesi ketika berhasil login
        function  getUser($nip,$password){ 
            $users=new Users();
            $pass=  md5($password);
            $sql=$this->connect->query("SELECT nip,nama,pass FROM d_pegawai WHERE nip='".$nip."' AND pass='".$pass."' AND id_statuspeg=1");
            $hasil=$sql->fetch();
            $jumlah=$sql->rowCount();
            if($hasil>0){
                $users->nip=$hasil['nip'];
                $users->nama=$hasil['nama'];
                $users->pass=$hasil['pass'];
                @session_start();
                $_SESSION['nip']=$nip;
                $_SESSION['nama']=$users->nama;
                $_SESSION['pass']=$password;
                $sql1=$this->connect->query("SELECT id_seksi as bagian FROM d_pegawai  WHERE nip=".$nip);
                $sql2=$this->connect->query("SELECT admin FROM d_pegawai WHERE admin='Y'AND nip=".$nip);
                $sql3=$this->connect->query("SELECT id_seksi FROM d_seksi WHERE pejabat='".$nip."' AND status_pejabat=''");
                if($sql1 AND $sql2 AND $sql3){
                    $row=$sql1->fetch();
                    $_SESSION['bagian']=$row['bagian'];
                    $users->bagian=$row['bagian'];
                        if(($sql2->rowCount())>0){
                            $_SESSION['admin']='Y';
                            $users->admin='Y';
                        }else{
                            $_SESSION['admin']='T'; 
                            $users->admin='T';
                        }
                            if(($sql3->rowCount())>0){
                                $row3=$sql3->fetch();
                                $_SESSION['pejabat']=$row3['id_seksi'];
                                $users->jabatan=$row3['id_seksi'];
                            }else{
                                $_SESSION['pejabat']='T';
                                $users->jabatan='T';
                            }
                }
            } 
            return $jumlah;;
        }
        
        //fungsi untuk merubah password
        function updatePassword($nip,$password){
             $this->nip=$nip;
             $this->pass=$password;
             $this->nama=$_SESSION['nama'];
             $this->admin=$_SESSION['admin'];
             $this->bagian=$_SESSION['bagian'];
             $sql=$this->connect->prepare("UPDATE d_pegawai SET pass= ?, log_update=CURDATE() WHERE nip= ?");
             $isi=array(md5($this->pass),$this->nip);
             $sql->execute($isi);
             $jumlah=$sql->rowCount();
             if($sql){
                 session_destroy();
                 @session_start();
                 $_SESSION['nip']=$this->nip;
                 $_SESSION['pass']=$this->pass;
                 $_SESSION['nama']=$this->nama;
                 $_SESSION['admin']=$this->admin;
                 $_SESSION['bagian']=$this->bagian;
                 $_SESSION['pejabat']=$this->jabatan;
             }
             return $jumlah;
        }
	
        
        //fungsi untuk mendapatkan role
        //digunakan untuk mengganti role apabila ditunjuk menjadi pjs
        function getRole($sesi_nip){
        $data=array();
        $sql=$this->connect->query("SELECT CONCAT(id_seksi,',',alias_jabatan) as role FROM d_seksi WHERE pejabat='".$sesi_nip."'");
            if($sql){
                while($row=$sql->fetch()){
                        $user=new Users();
                        $user->role=$row['role'];
                        $data[]=$user;
                    };    
            }
            return $data;
        }
        
        function setRole($jabatan){
            $this->jabatan=$jabatan;
            @session_start();
            unset($_SESSION['pejabat']);
            $_SESSION['pejabat']=$this->jabatan;
            if(isset($_SESSION['pejabat']))return true;
        }
}